package com.jindi.appuser.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class DateUtil {
	public static String DEFAULT_FORMAT = "yyyyMMdd";
	public static Map<String ,String> getMonth(){
		Map<String ,String> map = new HashMap<String ,String>(); 
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); 
        String firstDay = "";
        String lastDay = "";
        //获取前月的第一天
        Calendar   cal_1=Calendar.getInstance();//获取当前日期 
        cal_1.add(Calendar.MONTH, -1);
        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
        firstDay = format.format(cal_1.getTime());
        
        //获取前月的最后一天
        Calendar cale = Calendar.getInstance();   
        cale.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天 
        lastDay = format.format(cale.getTime());
          
          
        //获取当前月第一天：
        Calendar c = Calendar.getInstance();    
        c.add(Calendar.MONTH, 0);
        c.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
        String first = format.format(c.getTime());
          
        //获取当前月最后一天
        Calendar ca = Calendar.getInstance();    
        ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));  
        String last = format.format(ca.getTime());
        map.put("lastMonthStart", firstDay+"000000");
        map.put("lastMonthEnd", lastDay+"235959");
        map.put("thisMonthStart", first+"000000");
        map.put("thisMonthEnd", last+"235959");
        return map;
	}
	public static Map<String ,String> getThisWeek(){
		Map<String ,String> map = new HashMap<String ,String>();
		Calendar cal = Calendar.getInstance();  
		Date now = new Date(); 
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(dateFormat.format(now)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int d = 0;
	    if(cal.get(Calendar.DAY_OF_WEEK)==1){  
	        d = -6;  
	    }else{  
	        d = 2-cal.get(Calendar.DAY_OF_WEEK);  
	    }  
	    cal.add(Calendar.DAY_OF_WEEK, d);  
        map.put("startTime", new SimpleDateFormat("yyyyMMdd").format(cal.getTime())+"000000");
        cal.add(Calendar.DAY_OF_WEEK, 6); 
        map.put("endTime", new SimpleDateFormat("yyyyMMdd").format(cal.getTime())+"235959");
        return map;
	}
	public static Map<String ,String> getLastWeek(){
		Map<String ,String> map = new HashMap<String ,String>();
		Calendar cal = Calendar.getInstance();  
		Date now = new Date(); 
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            cal.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(dateFormat.format(now)));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int d = 0;  
        if(cal.get(Calendar.DAY_OF_WEEK)==1){  
            d = -6;  
        }else{  
            d = 2-cal.get(Calendar.DAY_OF_WEEK);  
        }  
        cal.add(Calendar.DAY_OF_WEEK, d-7);  
        //所在周开始日期  
        map.put("startTime", new SimpleDateFormat("yyyyMMdd").format(cal.getTime())+"000000");
        cal.add(Calendar.DAY_OF_WEEK, 6); 
        map.put("endTime", new SimpleDateFormat("yyyyMMdd").format(cal.getTime())+"235959");
        return map;
	}
	
	public static Map<String ,String> getYestoday(){
		Calendar cal=Calendar.getInstance();
		//System.out.println(Calendar.DATE);//5
		cal.add(Calendar.DATE,-1);
		Date time=cal.getTime();
		Map<String ,String> map = new HashMap<String ,String>();
		map.put("startTime", new SimpleDateFormat("yyyyMMdd").format(time)+"000000");
		map.put("endTime", new SimpleDateFormat("yyyyMMdd").format(time)+"235959");
		return map;
	}
	public static Map<String ,String> getToday(){
		Map<String ,String> map = new HashMap<String ,String>();
		Date now = new Date(); 
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		map.put("startTime", dateFormat.format(now)+"000000");
		map.put("endTime", dateFormat.format(now)+"235959");
		return map;
	}
	
	/** 
     * 格式化日期 
     * @param date 日期对象 
     * @return String 日期字符串 
     */  
    public static String formatDate(Date date){  
        SimpleDateFormat f = new SimpleDateFormat(DEFAULT_FORMAT);  
        String sDate = f.format(date);  
        return sDate;  
    }  
      
    /** 
     * 获取当年的第一天 
     * @param year 
     * @return 
     */  
    public static Date getCurrYearFirst(){  
        Calendar currCal=Calendar.getInstance();    
        int currentYear = currCal.get(Calendar.YEAR);  
        return getYearFirst(currentYear);  
    }  
      
    /** 
     * 获取当年的最后一天 
     * @param year 
     * @return 
     */  
    public static Date getCurrYearLast(){  
        Calendar currCal=Calendar.getInstance();    
        int currentYear = currCal.get(Calendar.YEAR);  
        return getYearLast(currentYear);  
    }  
      
    /** 
     * 获取某年第一天日期 
     * @param year 年份 
     * @return Date 
     */  
    public static Date getYearFirst(int year){  
        Calendar calendar = Calendar.getInstance();  
        calendar.clear();  
        calendar.set(Calendar.YEAR, year);  
        Date currYearFirst = calendar.getTime();  
        return currYearFirst;  
    }  
      
    /** 
     * 获取某年最后一天日期 
     * @param year 年份 
     * @return Date 
     */  
    public static Date getYearLast(int year){  
        Calendar calendar = Calendar.getInstance();  
        calendar.clear();  
        calendar.set(Calendar.YEAR, year);  
        calendar.roll(Calendar.DAY_OF_YEAR, -1);  
        Date currYearLast = calendar.getTime();  
          
        return currYearLast;  
    }

    /**
     * 获取多少（minute）分钟前的时间
     * @param minute
     * @return
     */
    public static Date getLastMinute(int minute){
        Calendar calendar = Calendar.getInstance();
        //calendar.clear();
        calendar.set(Calendar.MINUTE,calendar.get(Calendar.MINUTE)-minute);
        Date currYearLast = calendar.getTime();
        return currYearLast;
    }

}
